package me.magicall.web.simple.spring.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.magicall.convenient.ResultView;
import me.magicall.db.model.BaseModel.MapModel;
import me.magicall.db.outsea.ListSqlConfig;
import me.magicall.db.outsea.springjdbc.Count;
import me.magicall.db.outsea.springjdbc.GetList;
import me.magicall.web.simple.spring.util.C;
import me.magicall.web.simple.spring.util.U;
import me.magicall.web.simple.spring.util.UrlPatternRule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author MaGiCalL
 */
@Controller("listXmlController")
@RequestMapping(method = RequestMethod.GET, //
value = { UrlPatternRule.SINGULAR_MODEL_NAME + "s/xml",//
		UrlPatternRule.SINGULAR_MODEL_NAME + "/listXml" })
public class ListXmlController {

//	@Autowired
//	private MapModelAccessor mapModelAccessor;
	@Autowired
	private GetList<MapModel<?>> getList;
	@Autowired
	private Count<MapModel<?>> count;

	@SuppressWarnings("unchecked")
	@RequestMapping
	public String list(final HttpServletRequest request, final ModelMap modelMap,//
			@PathVariable("modelName") final String modelName) {//
		final Map<String, Object> paramterMap = (Map<String, Object>) request.getAttribute(C.REQUEST_PARAMS_NAME);// request.getParameterMap();
		final ListSqlConfig<MapModel<?>> sqlConfig = getList.createSqlConfig(modelName);
		sqlConfig.addConditions(paramterMap);
		final ResultView<MapModel<?>> rv = U.resultView(sqlConfig, getList, count);
//				mapModelAccessor.listWithPageInfo(modelName, paramterMap, C.ALL_PAGE_INFO);
		modelMap.addAttribute(modelName + "s", rv.getList());
		modelMap.addAttribute("modelName", modelName);
		return "ajax/" + modelName + "s";
	}
}
